﻿Imports ccapps.persistent.ServiceAssigner
Imports System.Collections.Generic


Public Class ServiceAssignerServiceSelectorSecond
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ContentPaneFooter1Second As ccapps.BillingNetControlLibrary.ContentPaneFooter
    Protected WithEvents LblokSecond As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid1Second As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblProcessIDSecond As System.Web.UI.WebControls.Label
    Protected WithEvents lblProcessTypeSecond As System.Web.UI.WebControls.Label
    Protected WithEvents lblContextHeaderSecond As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid1Temp As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim SelectedServices As List(Of String) = New List(Of String)
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PageTitle = "Service Assigner (Single)"

        If (Not Me.IsPostBack) Then
            Me.fillForm(0)
            Me.CheckTumDunya()
        End If
    End Sub

    Private Sub fillForm(ByVal pageIndex As Integer)
        Dim processID As String = Request.QueryString("ProcessID")

        Me.lblProcessTypeSecond.Text = persistent.ServiceAssigner.GetServiceAssignTypeDescription(persistent.ServiceAssigner.GetServiceAssignType(persistent.myDBConn.ORAScalar("select service_op_type from eu_bill.SERVICE_HANDLER_master_data where process_id = " & processID)))
        Me.lblProcessIDSecond.Text = processID

        Dim subOperationCode As Object = persistent.myDBConn.ORAScalar("select service_op_sub_type from eu_bill.service_handler_master_data where process_id = " & processID)
        DataGrid1Second.CurrentPageIndex = pageIndex
        Me.DataGrid1Second.DataSource = persistent.ServiceAssigner.GetServiceList(ServiceAssignType.Immediate, subOperationCode)
        Me.DataGrid1Second.DataBind()

        Me.DataGrid1Temp.DataSource = persistent.ServiceAssigner.GetServiceList(ServiceAssignType.Immediate, subOperationCode)
        Me.DataGrid1Temp.DataBind()



        RegisterScript("var forceToSingleSelection = " & ((Not IsDBNull(subOperationCode)) AndAlso subOperationCode.ToString().ToUpper() = "3G").ToString().ToLower() & ";", Me)
    End Sub
    Private Sub CheckTumDunya()
        Dim processID As String = Request.QueryString("ProcessID")
        Dim detailRows As DataTable = persistent.myDBConn.myORATable("select co_id, msisdn from eu_bill.service_handler_detail_data where process_id = " & processID)
        For Each dr As DataRow In detailRows.Rows
            If Get_Customer_Has_Tum_Dunya_Service(dr("CO_ID").ToString()) Then
                If Me.CurrentAssignProcessScope = ServiceAssignProcessScope.SINGLE Then
                    RegisterScript("var tumdunya = 'SINGLE';", Me)
                Else
                    RegisterScript("var tumdunya = 'NOTSINGLE';", Me)
                End If

                Return
            End If
        Next
        RegisterScript("var tumdunya = 'NOK';", Me)
    End Sub

    Private Sub ContentPaneFooter1Second_Confirmed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1Second.Confirmed
        Me.LblokSecond.Text = ""
        Dim processID As String = Request.QueryString("ProcessID")
        Dim detailRows As DataTable = persistent.myDBConn.myORATable("select co_id, msisdn from eu_bill.service_handler_detail_data where process_id = " & processID)
        Dim chkSelect As CheckBox
        Dim rlstProcessType As RadioButtonList

        If Me.CurrentAssignProcessScope = ServiceAssignProcessScope.SINGLE Then
            Dim service_op_type As String = persistent.myDBConn.ORAScalar("select service_op_type from eu_bill.SERVICE_HANDLER_master_data where process_id = " & processID)
            For Each dr As DataRow In detailRows.Rows
                For Each item As DataGridItem In Me.DataGrid1Second.Items
                    chkSelect = item.FindControl("chkSelect")
                    If (Not chkSelect Is Nothing) AndAlso chkSelect.Checked Then
                        Dim paramters(5) As String
                        paramters(0) = "p_sncode"
                        paramters(1) = "p_co_id"
                        paramters(2) = "p_service_op_type"
                        paramters(3) = "p_op_type"
                        paramters(4) = "p_request_source"
                        paramters(5) = "Message"

                        Dim paramters_value(5) As String

                        paramters_value(0) = item.Cells(2).Text

                        paramters_value(1) = dr("CO_ID").ToString()

                        paramters_value(2) = service_op_type
                        paramters_value(3) = CType(item.FindControl("rlstProcessType"), RadioButtonList).SelectedValue
                        paramters_value(4) = "NULL"
                        paramters_value(5) = String.Empty

                        persistent.myDBConn.ORAProcedure_INOUT("eu_bill.SERVICE_HANDLER.Check_ghh_sncode", paramters, paramters_value)

                        If paramters_value(5) <> "OK" Then
                            Alert(paramters_value(5), Me.Page)
                            Return
                        End If
                    End If
                Next
            Next

        End If

        Try
            Dim query As String = "INSERT INTO EU_BILL.SERVICE_HANDLER_DETAIL_DATA(PROCESS_ID, SEQNO, STATUS, STATUS_DATE, SNCODE, CS_SEQNO, OP_TYPE, MSISDN, CO_ID, BACKDATE_DATE) " & _
            "VALUES(" & _
            vbNewLine & processID & "," & _
            vbNewLine & "[SEQNO]" & "," & _
            vbNewLine & "'LOADED'" & "," & _
            vbNewLine & "SYSDATE" & "," & _
            vbNewLine & "[SNCODE], " & _
            vbNewLine & "[CSSEQNO], " & _
            vbNewLine & "'[OP_TYPE]', " & _
            vbNewLine & "'[MSISDN]', " & _
            vbNewLine & "[COID], " & _
            vbNewLine & "[BACKDATE] " & _
            ")", query2 As String
            Dim seqNo As Integer = 0

            If Not Me.Session("sncode") Is Nothing Then
                SelectedServices = TryCast(Session("sncode"), List(Of String))
                For Each item As DataGridItem In Me.DataGrid1Second.Items
                    chkSelect = item.FindControl("chkSelect")
                    If (Not chkSelect Is Nothing) AndAlso chkSelect.Checked Then
                        SelectedServices.Add(item.Cells(2).Text.ToString())
                    End If
                Next

            End If
            For Each item As DataGridItem In Me.DataGrid1Second.Items
                chkSelect = item.FindControl("chkSelect")
                If (Not chkSelect Is Nothing) AndAlso chkSelect.Checked Then
                    SelectedServices.Add(item.Cells(2).Text.ToString())
                End If
            Next
            For Each dr As DataRow In detailRows.Rows
                For Each item As DataGridItem In Me.DataGrid1Temp.Items

                    If (SelectedServices.Contains(item.Cells(2).Text.ToString())) Then
                        seqNo += 1
                        If seqNo = 1 Then
                            persistent.myDBConn.ORAExecute("DELETE FROM EU_BILL.SERVICE_HANDLER_DETAIL_DATA WHERE process_id = " & processID)
                        End If
                        query2 = query.Replace("[SEQNO]", seqNo.ToString())
                        query2 = query2.Replace("[SNCODE]", item.Cells(2).Text)
                        query2 = query2.Replace("[OP_TYPE]", CType(item.FindControl("rlstProcessType"), RadioButtonList).SelectedValue)
                        query2 = query2.Replace("[MSISDN]", dr("MSISDN").ToString())
                        query2 = query2.Replace("[COID]", dr("CO_ID").ToString())
                        query2 = query2.Replace("[CSSEQNO]", "NULL")
                        query2 = query2.Replace("[BACKDATE]", "NULL")
                        If persistent.myDBConn.ORAExecute(query2) < 1 Then
                            Throw New Exception("Bilgiler kaydedilirken hata oluştu!")
                        End If
                    End If
                    'chkSelect = item.FindControl("chkSelect")
                    'If (Not chkSelect Is Nothing) AndAlso chkSelect.Checked Then
                    '    seqNo += 1
                    '    If seqNo = 1 Then
                    '        persistent.myDBConn.ORAExecute("DELETE FROM EU_BILL.SERVICE_HANDLER_DETAIL_DATA WHERE process_id = " & processID)
                    '    End If
                    '    query2 = query.Replace("[SEQNO]", seqNo.ToString())
                    '    query2 = query2.Replace("[SNCODE]", item.Cells(2).Text)
                    '    query2 = query2.Replace("[OP_TYPE]", CType(item.FindControl("rlstProcessType"), RadioButtonList).SelectedValue)
                    '    query2 = query2.Replace("[MSISDN]", dr("MSISDN").ToString())
                    '    query2 = query2.Replace("[COID]", dr("CO_ID").ToString())
                    '    query2 = query2.Replace("[CSSEQNO]", "NULL")
                    '    query2 = query2.Replace("[BACKDATE]", "NULL")
                    '    If persistent.myDBConn.ORAExecute(query2) < 1 Then
                    '        Throw New Exception("Bilgiler kaydedilirken hata oluştu!")
                    '    End If
                    'End If
                Next
            Next

            Me.LblokSecond.Text = ""
            If seqNo = 0 Then
                Me.LblokSecond.Text = "Hiç servis seçmediniz! Lütfen işlem yapılacak en az 1 servis seçiniz.<BR>"
                Return
            ElseIf seqNo > 1000 Then
                persistent.myDBConn.ORAExecute("UPDATE EU_BILL.SERVICE_HANDLER_MASTER_DATA SET REQUESTER_EMAIL = NULL WHERE process_id = " & processID)
                Me.LblokSecond.Text = "Yüksek miktarlı giriş nedeniyle mail atılmayacak!<BR>"
            Else
                Me.LblokSecond.Text &= "Normal şartlarda process'iniz bitince mail alacaksınız. Ancak maillerle ilgili bir problem olması riskine karşı process'in rapor sayfaları incelenerek bir problem olması durumunda support grubuna case açılmalıdır!"
                Session.Remove("sncode")
            End If
            Me.ContentPaneFooter1Second.ConfirmButton.Visible = False
            persistent.ServiceAssigner.ConfirmRequest(processID)
            If Me.CurrentAssignProcessScope = ServiceAssignProcessScope.SINGLE Then
                persistent.myDBConn.ORAExecute("UPDATE EU_BILL.SERVICE_HANDLER_MASTER_DATA SET request_final_status = 'SINGLE_ORDERED' WHERE process_id = " & processID)
                persistent.myDBConn.ORAProcedure("eu_bill.service_handler.STARTPROCESS_SINGLE", New String() {"p_process_id"}, New String() {processID})
            End If
            Me.LblokSecond.Text += "<BR>Lütfen Request Numarası'nı kaydediniz ve işlemin statüsünü report sayfalarından takip ediniz."
        Catch ex As Exception
            RegisterScript("window.setTimeout('location.href=\'" & Me.Referrer & "\'', 10000);", Me)
            persistent.myDBConn.ORAExecute("DELETE FROM EU_BILL.SRVCE_HANDLER_REQUEST_HISTORY WHERE process_id = " & processID)
            persistent.myDBConn.ORAExecute("DELETE FROM EU_BILL.SERVICE_HANDLER_DETAIL_DATA WHERE process_id = " & processID)
            persistent.myDBConn.ORAExecute("DELETE FROM EU_BILL.SERVICE_HANDLER_MASTER_DATA WHERE process_id = " & processID)
            Me.LblokSecond.Text = "Aşağıdaki belirtilen hata oluştu, 10 sn sonra giriş ekranına yönlendirileceksiniz, lütfen işleminizi tekrarlamayı deneyiniz! <br><br>" & ex.Message
            Me.ContentPaneFooter1Second.ConfirmButton.Visible = False
        End Try

    End Sub

    Dim processTypeLiteral As String = String.Empty
    Public ReadOnly Property DefaultProcessType() As String
        Get
            If processTypeLiteral = String.Empty Then
                processTypeLiteral = persistent.myDBConn.ORAScalar("select nvl(op_type, 'NULL') from eu_bill.service_handler_master_data where process_id = " & Request.QueryString("ProcessID"))
            End If
            Return processTypeLiteral
        End Get
    End Property

    Public Property Referrer() As String
        Get
            If Me.CurrentAssignProcessScope = ServiceAssignProcessScope.LA Then
                Return "ServiceAssignerLAStep1.aspx"
            ElseIf Me.CurrentAssignProcessScope = ServiceAssignProcessScope.SME Then
                Return "ServiceAssignerSMEStep1.aspx"
            ElseIf Me.CurrentAssignProcessScope = ServiceAssignProcessScope.MSISDN Then
                Return "ServiceAssignerBulkStep1.aspx"
            ElseIf Me.CurrentAssignProcessScope = ServiceAssignProcessScope.SINGLE Then
                Return "ServiceAssignerSingleStep1.aspx"
            End If
        End Get
        Set(ByVal Value As String)
            Me.ViewState("__Referrer") = Value
        End Set
    End Property


    Private ReadOnly Property CurrentAssignProcessScope() As persistent.ServiceAssigner.ServiceAssignProcessScope
        Get
            If Me.ViewState("__AssignerProcessScope") Is Nothing Then
                Me.ViewState("__AssignerProcessScope") = persistent.myDBConn.ORAScalar("select process_type from eu_bill.SERVICE_HANDLER_master_data where process_id = " & Request.QueryString("ProcessID"))
            End If
            Return CType(System.Enum.Parse(GetType(persistent.ServiceAssigner.ServiceAssignProcessScope), Me.ViewState("__AssignerProcessScope")), persistent.ServiceAssigner.ServiceAssignProcessScope)
        End Get
    End Property

    Private Sub DataGrid1Second_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1Second.ItemCreated
        Dim chkSelect As CheckBox = e.Item.FindControl("chkSelect")
        If chkSelect Is Nothing Then
            Return
        End If
        chkSelect.Attributes("onclick") = "OnCheckboxClick(this);"
    End Sub

    Private Sub DataGrid1Second_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1Second.PageIndexChanged
        Dim processID As String = Request.QueryString("ProcessID")
        'Dim detailRows As DataTable = persistent.myDBConn.myORATable("select co_id, msisdn from eu_bill.service_handler_detail_data where process_id = " & processID)
        Dim chkSelect As CheckBox
        For Each item As DataGridItem In Me.DataGrid1Second.Items
            chkSelect = item.FindControl("chkSelect")
            If (Not chkSelect Is Nothing) AndAlso chkSelect.Checked Then
                If Not Me.Session("sncode") Is Nothing Then
                    SelectedServices = TryCast(Session("sncode"), List(Of String))
                End If
                SelectedServices.Add(item.Cells(2).Text.ToString())
                Session("sncode") = SelectedServices
                'Response.Write(item.Cells(2).Text.ToString)
            End If
        Next
        fillForm(e.NewPageIndex)
    End Sub
End Class
